package cn.restcloud.springboot.config;

import feign.RequestTemplate;
import feign.codec.EncodeException;
import feign.codec.Encoder;
import java.lang.reflect.Type;
import java.util.Map;

public class FeignFormEncoder implements Encoder {
    @Override
    public void encode(Object obj, Type type, RequestTemplate rt) throws EncodeException {
    	if(obj instanceof Map) {
    		//如果是map类型则认为是表单提交，进行编码处理
//	        rt.header("Content-Type", "application/x-www-form-urlencoded");
	        StringBuilder postStr=new StringBuilder();
	        Map<String,String> mapParams=(Map<String,String>)obj;
	        for(String key:mapParams.keySet()) {
	        	if(postStr.length()>0) {postStr.append("&");}
	        	postStr.append(key+"="+mapParams.get(key));
	        }
//	        PrintUtil.o("postStr=="+postStr);
	        rt.body(postStr.toString());
    	}
    }
}